package de.appengine.facebook.api.client;

import java.util.List;

import de.appengine.facebook.api.shared.model.FacebookUser;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Facebook_API_for_AppEngine implements EntryPoint {
	
	private static FacebookGraphAPIServiceAsync facebookService = GWT.create(FacebookGraphAPIService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		Button getFriends = new Button("getFrients");
		RootPanel.get().add(getFriends);
		getFriends.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				facebookService.getFriends(null, new AsyncCallback<List<FacebookUser>>() {
					
					@Override
					public void onSuccess(List<FacebookUser> result) {
						CellList<FacebookUser> cellList = new CellList<FacebookUser>(new FacebookUserCell());
						cellList.setRowData(result);
						RootPanel.get().add(cellList);
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				}
				);
			}
		});
	}
	
	private class FacebookUserCell extends AbstractCell<FacebookUser>{

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				FacebookUser value, SafeHtmlBuilder sb) {
			sb.appendEscaped(value.getId() + value.getName());
		}
		
	}
}
